package com.bw.culb_compose

import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.NavigationBar
import androidx.compose.material3.NavigationBarItem
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.painterResource
import androidx.navigation.NavDestination.Companion.hierarchy
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.currentBackStackEntryAsState
import androidx.navigation.compose.rememberNavController
import com.bw.clup_compose.R


sealed class NavItem(val route:String,val title:String,val img:Int){
    data object HomePage:NavItem("home","首页", R.drawable.ic_launcher_background)
    data object PlayCarPage:NavItem("playcar","玩车", R.drawable.ic_launcher_background)
    data object LookCarPage:NavItem("lookcar","看车", R.drawable.ic_launcher_background)
    data object MessagePage:NavItem("msg","消息", R.drawable.ic_launcher_background)
    data object MinePage:NavItem("mine","我的", R.drawable.ic_launcher_background)
}
@Composable
fun MainPage(modifier: Modifier = Modifier) {
    val list = listOf(NavItem.HomePage,NavItem.PlayCarPage,NavItem.LookCarPage,NavItem.MessagePage,NavItem.MinePage)
    val navHostController = rememberNavController()
    val StackEntry by navHostController.currentBackStackEntryAsState()
    val destination = StackEntry?.destination
    Scaffold(bottomBar = {
        if(destination?.hierarchy?.any {
                list.map { navItem -> navItem.route }.contains(it.route)
            }==true){
            NavigationBar(containerColor = Color.White) {
                list.forEach {
                    NavigationBarItem(selected = false, onClick = {
                        navHostController.navigate(it.route)
                    }, icon = {
                        Image(painter = painterResource(it.img), contentDescription = "")
                    }, label = {
                        Text(text = it.title)
                    })
                }
            }
        }
    }) {
        NavHost(
            navController = navHostController,
            startDestination = "home",
            modifier = Modifier.padding(it),
            builder = {
                composable("home") { HomePage() }
                composable("playcar") { PlayCarPage() }
                composable("lookcar") { LookCarPage() }
                composable("msg") { MessagePage() }
                composable("mine") { MinePage() }
            })
    }
}